﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IFRS.Web;
using IFRS.DataAccess.DAL;
using IFRS.Models;

public partial class IFRS_SEC03_3A : PageBase
{
    #region "---------- Events -----------"
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!(Page.IsPostBack || Page.IsCallback))
        {
            this.ViewMenu();
            SetErrorMessage();
            AddClientScript();
        }
        else
            ClientScript.RegisterHiddenField("isPostBack", "1");
    }
    protected void btnSAVE_Click(object sender, EventArgs e)
    {
        SaveData(false);
    }
    protected void btnSAVE_NEW_Click(object sender, EventArgs e)
    {
        SaveData(true);
    }
    protected void btnCANCEL_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/IFRS/SEC03_1S.aspx");
    }
    protected void btnCLEAR_Click(object sender, EventArgs e)
    {
        Response.Redirect(ResolveUrl(Request.Url.AbsoluteUri));
    }
    #endregion
    #region "----- Methods -------"
    private void ViewMenu()
    {
        this.MenuTitleBar = Resources.ifrsMenu.SEC01_1S;
    }
    private void SetErrorMessage()
    {
        //reqEmployee_ID.ErrorMessage = "*";
        //reqGroup.ErrorMessage = "*";
        btnSAVE.ValidationGroup = "Master";
        btnSAVE_NEW.ValidationGroup = "Master";
        reqEmployee_ID.SetRequiredField(lblEmployee_ID, txtEmployee_ID, btnSAVE);
        reqGroup.SetRequiredField(lblGroup, txtGroup, btnSAVE);
    }

    private void AddClientScript()
    {
        btnEmployee.Attributes.Add("onClick", "return popWindow('" + ResolveUrl("~/PopupPages/ADUser.aspx") + "','700','550');");
        btnGroup.Attributes.Add("onClick", "return popWindow('" + ResolveUrl("~/PopupPages/RoleGroup.aspx") + "','700','550');");
        //btnGLAccountCode.Attributes.Add("onClick", "return popWindow('" + ResolveUrl("~/PopupPages/GLAccount.aspx") + "','700','550');");

        txtEmployee_ID.AddControl(txtEmployee_ID.ClientID);
        txtEmployee_ID.AddControl(txtEmployee_Name.ClientID);
        txtEmployee_ID.AddControl(hidCostCenter.ClientID);
        txtEmployee_ID.AddControl(hidEmployee_Name.ClientID);
        txtEmployee_ID.AddControl(txtCostCenter.ClientID);
        txtEmployee_ID.ControlSearch = txtEmployee_ID.ClientID;
        txtEmployee_ID.SearchKey = "ADUSER_oEmployeeID_oDescription_oPostalCode_oName_oCOST_CENTER_NM";


        txtGroup.AddControl(hidGroup_ID.ClientID);
        txtGroup.AddControl(txtGroup.ClientID);
        txtGroup.ControlSearch = txtGroup.ClientID;
        txtGroup.SearchKey = "RoleGroup_oRoleGroupID_oRoleGroupName";
    }

    private void SaveData(bool SaveNew)
    {

        try
        {
            RoleGroupUser vRoleGroupUser = new RoleGroupUser();
            vRoleGroupUser.EmployeeCode = txtEmployee_ID.Text;
            vRoleGroupUser.RoleGroupID = hidGroup_ID.Value.AsInt32();
            vRoleGroupUser.Username = hidEmployee_Name.Value.ToString();
            if (rbSTATUS_1.Checked)
            {
                vRoleGroupUser.IsActive = true;
            }
            else
            {
                vRoleGroupUser.IsActive = false;
            }
            vRoleGroupUser.SetStandardField();
            //vRoleGroupUser.CreatedDate = DateTime.Now;
            //vRoleGroupUser.CreatedBy = SessionManager.IFRS_EMPLOYEE_ID;
            //vRoleGroupUser.CreatedCostCenter = SessionManager.IFRS_COST_CENTER;
            //vRoleGroupUser.CreatedBranch = SessionManager.IFRS_BRANCH;
            //vRoleGroupUser.UpdatedDate = DateTime.Now;
            //vRoleGroupUser.UpdatedBy = SessionManager.IFRS_EMPLOYEE_ID;
            //vRoleGroupUser.UpdatedCostCenter = SessionManager.IFRS_COST_CENTER;
            //vRoleGroupUser.UpdatedBranch = SessionManager.IFRS_BRANCH;

            var vROLEGROUPUSER = new SPR_ROLEGROUPUSER();
            int iID = vROLEGROUPUSER.INSERT(vRoleGroupUser);
            if (iID > 0)
            {

                if (SaveNew)
                {
                    this.JS_Alert(Resources.ifrsResource.SaveData, ResolveUrl("~/IFRS/SEC03_3A.aspx"));
                }
                else
                {
                    this.JS_Alert(Resources.ifrsResource.SaveData, ResolveUrl(string.Format("~/IFRS/SEC03_2E.aspx?&id={0}", iID)));
                }
            }
            else
            {
                Utility.AlertMessage(Page, Resources.ifrsResource.CanNotSaveData);
            }

        }
        catch (Exception ex)
        {
            throw ex;
        }

    }
    #endregion
}